﻿using DuckDB.NET.Data;
using Newtonsoft.Json;
using RPAAction.Data_CSO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RPAAction
{
    class Run
    {
        internal static void Run_(string[] p)
        {
            if (p.Length > 1)
            {
                if (p[1].Equals("SqlServerToExcel"))
                {
                    SqlServerToExcel(p);
                }
                else if (p[1].Equals("SqlServerToTXT"))
                {
                    SqlServerToTXT(p);
                }
                else if (p[1].Equals("SqlServerToDuckDB"))
                {
                    SqlServerToDuckDB(p);
                }
                else if (p[1].Equals("ExcelToSqlServer"))
                {
                    ExcelToSqlServer(p);
                }
                else if (p[1].Equals("RFCToSqlServer"))
                {
                    RFCToSqlServer(p);
                }
                else if (p[1].Equals("RFCToDuckDB"))
                {
                    RFCToDuckDB(p);
                }
                else if (p[1].Equals("ExcelToDuckDB"))
                {
                    ExcelToDuckDB(p);
                }
                else if (p[1].Equals("DuckDBToExcel"))
                {
                    DuckDBToExcel(p);
                }
                else if (p[1].Equals("DuckDBToJson"))
                {
                    DuckDBToJson(p);
                }
                else if (p[1].Equals("DuckDBExecNonQuery"))
                {
                    DuckDBExecNonQuery(p);
                }
                else if (p[1].Equals("CloseExcel"))
                {
                    CloseExcel(p);
                }
            }
            else
            {
                Console.WriteLine("V-" + Tool.Version_s);
                Console.WriteLine("SqlServerToExcel");
                Console.WriteLine("SqlServerToTXT");
                Console.WriteLine("SqlServerToDuckDB");
                Console.WriteLine("ExcelToSqlServer");
                Console.WriteLine("RFCToDuckDB");
                Console.WriteLine("RFCToSqlServer");
                Console.WriteLine("ExcelToDuckDB");
                Console.WriteLine("DuckDBToJson");
                Console.WriteLine("DuckDBExecNonQuery");
                Console.WriteLine("CloseExcel");
            }
        }

        static void SqlServerToExcel(string[] p)
        {
            if (p.Length < 3)
            {
                Console.WriteLine("DataSource\tip,prot");//2
                Console.WriteLine("DataBase\t数据库名称");//3
                Console.WriteLine("user\t\t用户");//4
                Console.WriteLine("pwd\t\t密码");//5
                Console.WriteLine("SQL");//6
                Console.WriteLine("ExcelPath");//7
                Console.WriteLine("sheet");//8
            }
            else
            {
                string connStr = $@"Data Source={p[2]};Initial Catalog={p[3]};User ID={p[4]};Pwd={p[5]};";
                using (SqlConnection connection = new SqlConnection(connStr))
                {
                    SqlCommand command = new SqlCommand(p[6], connection);
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    RPADataExport.ImportDispose(
                        reader,
                        new ExcelDataExport(p[7], p[8], "A1", true)
                    );
                }
            }
        }

        static void SqlServerToTXT(string[] p)
        {
            if (p.Length < 3)
            {
                Console.WriteLine("DataSource\tip,prot");//2
                Console.WriteLine("DataBase\t数据库名称");//3
                Console.WriteLine("user\t\t用户");//4
                Console.WriteLine("pwd\t\t密码");//5
                Console.WriteLine("SQL");//6
                Console.WriteLine("Path");//7
            }
            else
            {
                string connStr = $@"Data Source={p[2]};Initial Catalog={p[3]};User ID={p[4]};Pwd={p[5]};";
                using (SqlConnection connection = new SqlConnection(connStr))
                {
                    SqlCommand command = new SqlCommand(p[6], connection);
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    RPADataExport.ImportDispose(
                        reader,
                        new TXTDataExport(p[7])
                    );
                }
            }
        }


        static void SqlServerToDuckDB(string[] p)
        {
            if (p.Length < 3)
            {
                Console.WriteLine("DataSource\tip,prot");//2
                Console.WriteLine("DataBase\t数据库名称");//3
                Console.WriteLine("user\t\t用户");//4
                Console.WriteLine("pwd\t\t密码");//5
                Console.WriteLine("SQL");//6
                Console.WriteLine("DuckDBPath");//7
                Console.WriteLine("Table");//8
            }
            else
            {
                string connStr = $@"Data Source={p[2]};Initial Catalog={p[3]};User ID={p[4]};Pwd={p[5]};";
                using (SqlConnection connection = new SqlConnection(connStr))
                {
                    SqlCommand command = new SqlCommand(p[6], connection);
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    RPADataExport.ImportDispose(
                        reader,
                        new DuckDbDataExport(p[7], p[8])
                    );
                }
            }
        }

        static void ExcelToSqlServer(string[] p)
        {
            if (p.Length < 3)
            {
                Console.WriteLine("DataSource\tip,prot");//2
                Console.WriteLine("DataBase\t数据库名称");//3
                Console.WriteLine("user\t\t用户");//4
                Console.WriteLine("pwd\t\t密码");//5
                Console.WriteLine("Table");//6
                Console.WriteLine("ExcelPath");//7
                Console.WriteLine("sheet");//8
                Console.WriteLine("TRUNCATE TABLE\t如果不是0则清空数据表");//9

                Console.WriteLine("tagTitle 用来标记导出的Excel数据");//10
                Console.WriteLine("tagValue");//11
            }
            else
            {
                RPADataExport.ImportDispose(
                    p.Length <= 10 ? new ExcelDataReader(p[7], p[8]) : new ExcelDataReaderWithTag(p[10], p[11], p[7], p[8]),
                    new SQLServerDataExport(p[2], p[3], p[4], p[5], p[6], p[9].Equals("0"))//超时时间半小时
                );
            }
        }

        static void RFCToSqlServer(string[] p)
        {
            if (p.Length < 3)
            {
                Console.WriteLine("appHost");//2
                Console.WriteLine("appSservice");//3
                Console.WriteLine("msgHost");//4
                Console.WriteLine("msgService");//5
                Console.WriteLine("user");//6
                Console.WriteLine("pwd");//7
                Console.WriteLine("client");//8
                Console.WriteLine("language");//9
                Console.WriteLine("timeout");//10
                Console.WriteLine("name");//11
                Console.WriteLine("funcName");//12
                Console.WriteLine("group");//13

                //下面的"//10"改成"//14","//11"改成"//15",以此类推

                Console.WriteLine("in_name1");//14
                Console.WriteLine("in_value1");//15
                Console.WriteLine("in_name2");//16
                Console.WriteLine("in_value2");//17
                Console.WriteLine("in_name3");//18
                Console.WriteLine("in_value3");//19
                Console.WriteLine("in_name4");//20
                Console.WriteLine("in_value4");//21
                Console.WriteLine("in_name5");//22
                Console.WriteLine("in_value5");//23

                Console.WriteLine("in_rfctable1");//24
                Console.WriteLine("in_excel1");//25
                Console.WriteLine("in_Sheet1");//26
                Console.WriteLine("in_rfctable2");//27
                Console.WriteLine("in_excel2");//28
                Console.WriteLine("in_Sheet2");//29
                Console.WriteLine("in_rfctable3");//30
                Console.WriteLine("in_excel3");//31
                Console.WriteLine("in_Sheet3");//32
                Console.WriteLine("in_rfctable4");//33
                Console.WriteLine("in_excel4");//34
                Console.WriteLine("in_Sheet4");//35
                Console.WriteLine("in_rfctable5");//36
                Console.WriteLine("in_excel5");//37
                Console.WriteLine("in_Sheet5");//38

                Console.WriteLine("DataSource");//39
                Console.WriteLine("DataBase");//40
                Console.WriteLine("user");//41
                Console.WriteLine("pwd");//42
                Console.WriteLine("timeout");//43

                Console.WriteLine("out_rfctable1");//44
                Console.WriteLine("out_table1");//45
                Console.WriteLine("appand1");//46
                Console.WriteLine("tagTitle1");//47
                Console.WriteLine("tagValue1");//48
                Console.WriteLine("out_rfctable2");//49
                Console.WriteLine("out_table2");//50
                Console.WriteLine("appand2");//51
                Console.WriteLine("tagTitle2");//52
                Console.WriteLine("tagValue2");//53
                Console.WriteLine("out_rfctable3");//54
                Console.WriteLine("out_table3");//55
                Console.WriteLine("appand3");//56
                Console.WriteLine("tagTitle3");//57
                Console.WriteLine("tagValue3");//58
                Console.WriteLine("out_rfctable4");//59
                Console.WriteLine("out_table4");//60
                Console.WriteLine("appand4");//61
                Console.WriteLine("tagTitle4");//62
                Console.WriteLine("tagValue4");//63
                Console.WriteLine("out_rfctable5");//64
                Console.WriteLine("out_table5");//65
                Console.WriteLine("appand5");//66
                Console.WriteLine("tagTitle5");//67
                Console.WriteLine("tagValue5");//68
            }
            else
            {
                CSRFC.CSRFC.SetRFCConf(p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13]);//修改后的代码


                //继续修改下面的代码,p[11]改成p[14],p[12]改成p[15],以此类推

                if (!p[14].Equals("")) CSRFC.CSRFC.SetValue(p[14], p[15]);
                if (!p[16].Equals("")) CSRFC.CSRFC.SetValue(p[16], p[17]);
                if (!p[18].Equals("")) CSRFC.CSRFC.SetValue(p[18], p[19]);
                if (!p[20].Equals("")) CSRFC.CSRFC.SetValue(p[20], p[21]);
                if (!p[22].Equals("")) CSRFC.CSRFC.SetValue(p[22], p[23]);

                if (!p[24].Equals("")) CSRFC.CSRFC.SetTableFromExcel(p[24], p[25], p[26]);
                if (!p[27].Equals("")) CSRFC.CSRFC.SetTableFromExcel(p[27], p[28], p[29]);
                if (!p[30].Equals("")) CSRFC.CSRFC.SetTableFromExcel(p[30], p[31], p[32]);
                if (!p[33].Equals("")) CSRFC.CSRFC.SetTableFromExcel(p[33], p[34], p[35]);
                if (!p[36].Equals("")) CSRFC.CSRFC.SetTableFromExcel(p[36], p[37], p[38]);

                CSRFC.CSRFC.Invoke();

                //------------------------------------------------------=**---------------------------------**----------------**------------------------**-----**---
                if (!p[44].Equals("")) CSRFC.CSRFC.GetTableToSqlServer(p[44], p[39], p[40], p[41], p[42], p[45], bool.Parse(p[46]), int.Parse(p[43]), p[47], p[48]);
                if (!p[49].Equals("")) CSRFC.CSRFC.GetTableToSqlServer(p[49], p[39], p[40], p[41], p[42], p[50], bool.Parse(p[51]), int.Parse(p[43]), p[52], p[53]);
                if (!p[54].Equals("")) CSRFC.CSRFC.GetTableToSqlServer(p[54], p[39], p[40], p[41], p[42], p[55], bool.Parse(p[56]), int.Parse(p[43]), p[57], p[58]);
                if (!p[59].Equals("")) CSRFC.CSRFC.GetTableToSqlServer(p[59], p[39], p[40], p[41], p[42], p[60], bool.Parse(p[61]), int.Parse(p[43]), p[62], p[63]);
                if (!p[64].Equals("")) CSRFC.CSRFC.GetTableToSqlServer(p[64], p[39], p[40], p[41], p[42], p[65], bool.Parse(p[66]), int.Parse(p[43]), p[67], p[68]);

            }
        }

        static void RFCToDuckDB(string[] p)
        {
            if (p.Length < 3)
            {
                Console.WriteLine("appHost");//2
                Console.WriteLine("appSservice");//3
                Console.WriteLine("msgHost");//4
                Console.WriteLine("msgService");//5
                Console.WriteLine("user");//6
                Console.WriteLine("pwd");//7
                Console.WriteLine("client");//8
                Console.WriteLine("language");//9
                Console.WriteLine("timeout");//10
                Console.WriteLine("name");//11
                Console.WriteLine("funcName");//12
                Console.WriteLine("group");//13

                Console.WriteLine("in_name1");//14
                Console.WriteLine("in_value1");//15
                Console.WriteLine("in_name2");//16
                Console.WriteLine("in_value2");//17
                Console.WriteLine("in_name3");//18
                Console.WriteLine("in_value3");//19
                Console.WriteLine("in_name4");//20
                Console.WriteLine("in_value4");//21
                Console.WriteLine("in_name5");//22
                Console.WriteLine("in_value5");//23

                Console.WriteLine("in_rfctable1");//24
                Console.WriteLine("in_excel1");//25
                Console.WriteLine("in_Sheet1");//26
                Console.WriteLine("in_rfctable2");//27
                Console.WriteLine("in_excel2");//28
                Console.WriteLine("in_Sheet2");//29
                Console.WriteLine("in_rfctable3");//30
                Console.WriteLine("in_excel3");//31
                Console.WriteLine("in_Sheet3");//32
                Console.WriteLine("in_rfctable4");//33
                Console.WriteLine("in_excel4");//34
                Console.WriteLine("in_Sheet4");//35
                Console.WriteLine("in_rfctable5");//36
                Console.WriteLine("in_excel5");//37
                Console.WriteLine("in_Sheet5");//38

                Console.WriteLine("DuckDBPath");//39

                Console.WriteLine("out_rfctable1");//40
                Console.WriteLine("out_table1");//41
                Console.WriteLine("tagTitle1");//42
                Console.WriteLine("tagValue1");//43
                Console.WriteLine("out_rfctable2");//44
                Console.WriteLine("out_table2");//45
                Console.WriteLine("tagTitle2");//46
                Console.WriteLine("tagValue2");//47
                Console.WriteLine("out_rfctable3");//48
                Console.WriteLine("out_table3");//49
                Console.WriteLine("tagTitle3");//50
                Console.WriteLine("tagValue3");//51
                Console.WriteLine("out_rfctable4");//52
                Console.WriteLine("out_table4");//53
                Console.WriteLine("tagTitle4");//54
                Console.WriteLine("tagValue4");//55
                Console.WriteLine("out_rfctable5");//56
                Console.WriteLine("out_table5");//57
                Console.WriteLine("tagTitle5");//58
                Console.WriteLine("tagValue5");//59
            }
            else
            {
                CSRFC.CSRFC.SetRFCConf(p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13]);

                if (!p[14].Equals("")) CSRFC.CSRFC.SetValue(p[14], p[15]);
                if (!p[16].Equals("")) CSRFC.CSRFC.SetValue(p[16], p[17]);
                if (!p[18].Equals("")) CSRFC.CSRFC.SetValue(p[18], p[19]);
                if (!p[20].Equals("")) CSRFC.CSRFC.SetValue(p[20], p[21]);
                if (!p[22].Equals("")) CSRFC.CSRFC.SetValue(p[22], p[23]);

                if (!p[24].Equals("")) CSRFC.CSRFC.SetTableFromExcel(p[24], p[25], p[26]);
                if (!p[27].Equals("")) CSRFC.CSRFC.SetTableFromExcel(p[27], p[28], p[29]);
                if (!p[30].Equals("")) CSRFC.CSRFC.SetTableFromExcel(p[30], p[31], p[32]);
                if (!p[33].Equals("")) CSRFC.CSRFC.SetTableFromExcel(p[33], p[34], p[35]);
                if (!p[36].Equals("")) CSRFC.CSRFC.SetTableFromExcel(p[36], p[37], p[38]);

                CSRFC.CSRFC.Invoke();

                if (!p[41].Equals("")) CSRFC.CSRFC.GetTableToDuckDB(p[40], p[39], p[41], p[42], p[43]);
                if (!p[45].Equals("")) CSRFC.CSRFC.GetTableToDuckDB(p[44], p[39], p[45], p[46], p[47]);
                if (!p[49].Equals("")) CSRFC.CSRFC.GetTableToDuckDB(p[48], p[39], p[49], p[50], p[51]);
                if (!p[53].Equals("")) CSRFC.CSRFC.GetTableToDuckDB(p[52], p[39], p[53], p[54], p[55]);
                if (!p[57].Equals("")) CSRFC.CSRFC.GetTableToDuckDB(p[56], p[39], p[57], p[58], p[59]);
            }
        }

        static void ExcelToDuckDB(string[] p)
        {
            if (p.Length < 3)
            {
                Console.WriteLine("dataSource\tDuckDb数据库位置");//2
                Console.WriteLine("Table");//3

                Console.WriteLine("ExcelPath");//4
                Console.WriteLine("sheet");//5

            }
            else
            {
                RPADataExport.ImportDispose(
                    p.Length <= 6 ? new ExcelDataReader(p[4], p[5]) : new ExcelDataReaderWithTag(p[6], p[7], p[4], p[5]),
                    new DuckDbDataExport(p[2], p[3])
                );
            }
        }

        static void DuckDBToExcel(string[] p)
        {
            if (p.Length < 3)
            {
                Console.WriteLine("dataSource\tDuckDb数据库位置");//2
                Console.WriteLine("SQL");//3

                Console.WriteLine("ExcelPath");//4
                Console.WriteLine("sheet");//5

            }
            else
            {
                RPADataExport.ImportDispose(
                    new DuckDbDataReader(p[2], p[3]),
                    new ExcelDataExport(p[4], p[5], "A1", true)
                );
            }
        }

        static void DuckDBToJson(string[] p)
        {
            if (p.Length < 3)
            {
                Console.WriteLine("dataSource\tDuckDb数据库位置");//2
                Console.WriteLine("SQL");//3

            }
            else
            {
                DataTable table = new DataTable();

                RPADataExport.ImportDispose(
                    new DuckDbDataReader(p[2], p[3]),
                    new DataTableDataExport(table)
                );

                string json = JsonConvert.SerializeObject(table);
                Console.OutputEncoding = Encoding.UTF8;
                Console.WriteLine(json);
            }
        }

        static void DuckDBExecNonQuery(string[] p)
        {
            if (p.Length < 3)
            {
                Console.WriteLine("dataSource\tDuckDb数据库位置");//2
                Console.WriteLine("SQL");//3

            }
            else
            {
                DuckDBConnection connection = new DuckDBConnection($"Data Source={p[2]};");
                connection.Open();
                DuckDbCommand command = new DuckDbCommand(p[3], connection);
                int affectedRows = command.ExecuteNonQuery();
                Console.WriteLine(affectedRows);
            }
        }

        static void CloseExcel(string[] p)
        {
            new Excel_CSO.Process_Close();
        }
    }
}
